package com.hkbigdata.springbootdi.mapper.impl;

import com.hkbigdata.springbootdi.mapper.ESDao;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;

/**
 * @author liuanbo
 * @creat 2024-12-13-14:01
 * @see 2194550857@qq.com
 */
@org.springframework.stereotype.Repository
/**
 * mapper 适用于jdbc mybatis
 * Repository 适用于所有数据库
 */
public class ESDaoImpl implements ESDao {

    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Override
    public <T> SearchHits<T> queryFromES(AbstractAggregationBuilder agg, Class<T> t) {

        //构建查询条件
        NativeSearchQuery query = new NativeSearchQueryBuilder().addAggregation(agg).build();
        SearchHits<T> searchHits = elasticsearchRestTemplate.search(query, t);
        return searchHits;
    }
}
